var mlControls = {
	holder: null,
	title: null,
	navClose: null,
	navOk: null,
	navCancel: null,
	content: null,
	message: null,
	controls: null,
	callback: null,
	
	options: null,

	getHolder : function(){
		
		mlControls.holder = $('<div/>')
			.addClass('mlm-controls')
			.addClass('mlm-holder')
			.hide()
			.appendTo('body');
			
		mlControls.title = $('<div/>').addClass('mlm-title').appendTo(mlControls.holder);	
		
		mlControls.content = $('<div/>').addClass('mlm-content').appendTo(mlControls.holder);	
		
		mlControls.message = $('<div/>').addClass('mlm-message').appendTo(mlControls.content);	
		
		mlControls.controls = $('<div/>').addClass('mlm-controls').appendTo(mlControls.content);	
		
		mlControls.navClose = $('<a/>')
			.addClass('mlm-nav-close')
			.text('X Fechar')
			.attr({
				href: '#',
				title: 'Fechar'
			})
			.appendTo(mlControls.holder);	
		
		mlControls.navOk = $('<a/>')
			.addClass('mlm-nav-ok')
			.appendTo(mlControls.controls);	

		mlControls.navCancel = $('<a/>')
			.addClass('mlm-nav-cancel')
			.text('Não')
			.attr({
				href: '#',
				title: 'Não'
			})
			.appendTo(mlControls.controls);	
	},

	alert: function(message, type, callback){
		if (callback != undefined) mlControls.callback = callback;

		if(mlControls.holder == null) mlControls.getHolder();
		
		mlControls.setMode('alert');
		
		if(mlModal.opened){
			mlModal.hide();
		}
		
		mlCurtain.open();
		mlControls.title.text('Atenção');
		mlControls.message.html	(message);
		mlControls.holder.css('margin-top', (mlControls.holder.height() / 2).round() * -1 )
		
		mlControls.holder.show();
	},
	
	confirm: function(message, callback, type){
		mlControls.callback = callback;
		
		if(mlControls.holder == null) mlControls.getHolder();
		
		mlControls.setMode('confirm');
		
		if(mlModal.opened){
			mlModal.hide();
		}
		
		mlCurtain.open();
		mlControls.title.text('Atenção');
		mlControls.message.html	(message);
		mlControls.holder.css('margin-top', (mlControls.holder.height() / 2).round() * -1 )
		
		mlControls.holder.show();
	},
	
	setMode: function(mode){
		trace('setMode : ' + mode);
		
		if(mode == 'alert'){
			mlControls.navClose
				.unbind('click')
				.bind('click', function(e){
					e.preventDefault();
					if(mlControls.callback != null) mlControls.callback();
					mlControls.close();
				});
			
			mlControls.navOk
				.text('Ok')
				.attr('title', 'Ok')
				.unbind('click')
				.bind('click', function(e){
					e.preventDefault();
					if(mlControls.callback != null) mlControls.callback();
					mlControls.close();
				});
			
			mlControls.navCancel.hide();
		
		}else if(mode == 'confirm'){
			
			mlControls.navOk
				.text('Sim')
				.attr('title', 'Sim')
				.unbind('click')
				.bind('click', function(e){
					e.preventDefault();
					mlControls.callback(true);
					mlControls.close();
				});

				mlControls.navCancel
					.show()
					.unbind('click')
					.bind('click', function(e){
						e.preventDefault();
						mlControls.callback(false);
						mlControls.close();
					});

				mlControls.navClose
					.unbind('click')
					.bind('click', function(e){
						e.preventDefault();
						mlControls.callback(false);
						mlControls.close();
					});
		}
	},

	close : function(){
		mlControls.callback = null;
		
		if(mlModal.opened){
			setTimeout(function(){
				if(mlModal.opened) mlModal.show();
			}, 300);
		}else{
			mlCurtain.close();
		}

		mlControls.holder.hide();
	}
};



var mlModal = {
	
	holder: null,
	title: null,
	navClose: null,
	content: null,
	opened: false,
	
	options: null,
	
	getHolder : function(attribute){
		mlModal.holder = $('<div/>')
			.addClass('mlm-holder')
			.addClass('mlm-modal')
			.width( 400 )
			.height( 400 )
			.hide()
			.appendTo('body');
			
		mlModal.title = $('<div/>').addClass('mlm-title').appendTo(mlModal.holder);	
		
		mlModal.content = $('<div/>').addClass('mlm-content').appendTo(mlModal.holder);	
		
		mlModal.navClose = $('<a/>')
			.addClass('mlm-nav-close')
			.text('X Fechar')
			.attr({
				href: '#',
				title: 'Fechar'
			})
			.bind('click', function(e){
				e.preventDefault();
				mlModal.close();
			})
			.appendTo(mlModal.holder);	
	},

	open: function(options) {
		if(mlModal.holder == null) mlModal.getHolder();
		
		mlModal.opened = true;
		
		var defaults = {
			title: 'Atenção',
			onOpen: null,
			onClose: null,
			elContent: null,
			urlContent: null,
			loadComplete: null,
			width: 980,
			height: 400,
			showClose: true
		};
		
		mlModal.options = $.extend( defaults, options );

		mlCurtain.open();
		mlModal.showLoading();	
		mlModal.title.text(mlModal.options.title);
		mlModal.content.height( mlModal.options.height - mlModal.title.height() );
		
		if(mlModal.options.elContent != null){
			mlModal.content.append( mlModal.options.elContent.show() );
			mlModal.hideLoading();	
		
		}else if(mlModal.options.urlContent != null){
			mlModal.content.load( mlModal.options.urlContent, function(){
				mlModal.hideLoading();	
				if(mlModal.options.loadComplete != null ) mlModal.options.loadComplete()
			});
		}else{
			trace('sem conteudo...');
			return;
		}	
		
		mlModal.holder
			.width( mlModal.options.width )
			.height( mlModal.options.height )
			.css({
				marginTop : (mlModal.options.height / 2) * -1,
				marginLeft: (mlModal.options.width  / 2) * -1,
			})
			.show();
	},
	
	hide : function(){
		mlModal.holder.hide();
	},
	
	show : function(){
		mlModal.holder.show();
	},
	
	close : function(){
		mlModal.opened = false;
		mlModal.holder.hide();
		mlCurtain.close();
	},
	
	showLoading: function(){
		mlModal.content.addClass('on-load');
	},
	
	hideLoading: function(){
		mlModal.content.removeClass('on-load');
	}
};


var mlCurtain = {
	el: null,
	created: false,
	
	create : function( ){
		mlCurtain.el = $('<div/>')
			.addClass('mlm-curtain')
			.height( $(window).height( ) )
			.fadeTo(0, .7)
			.hide()
			.appendTo('body');
		
		$(window).bind('resize', function(){
			mlCurtain.el.height( $(window).height( ) );
		});	
		
		mlCurtain.created = true;
	},
	
	open : function(options){
		if(!mlCurtain.created) mlCurtain.create( );
		mlCurtain.el.show();
	},
	
	close : function(options){
		mlCurtain.el.hide();
	}
};



var mlSerializer = function () {
	this.items = null;
	
	this.init = function() {
		this.items = new Array();
		return this;
	};
	
	this.add = function(label, value) {
		this.items.push('"{0}":"{1}"'.format(label, value));
		return this;
	};
	
	this.render = function() {
		return '{{0}}'.format(this.items.join(','));
	};
	
	this.init();
}